import redis.clients.jedis.Jedis;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;


public class RedisSavingPhoto {
    private JPanel mainpanel;
    private JLabel label1;
    private JButton button_showimg;
    private JButton button_clear;
    private JButton button_setkey;
    private JButton button_getkey;
    private static final String IMG_PATH = "hello.jpg";


    public RedisSavingPhoto() {
        button_showimg.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    BufferedImage img = ImageIO.read(new File(IMG_PATH));
                    ImageIcon icon = new ImageIcon(img);
                    label1.setIcon(icon);
//                    JOptionPane.showMessageDialog(null,"加载成功 hello.jpg");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        button_clear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label1.setIcon(null);
                JOptionPane.showMessageDialog(null,"清除图像完毕");
            }
        });
        button_setkey.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Jedis jedis = new Jedis("localhost");
                try {
                    jedis.set("image01".getBytes(), ImageToByteArray());
                    JOptionPane.showMessageDialog(null,"存入成功");
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                    JOptionPane.showMessageDialog(null,"请检查是否没有开启localhost：6739的redis服务");
                }
            }
        });
        button_getkey.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取byte array类型的数据,转化为图片bufferimage,显示
                // 这里有问题
                try {
//                    BufferedImage bImage = ImageIO.read(new File(IMG_PATH));
//                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
//                    ImageIO.write(bImage, "jpg", bos);
//                    byte[] data = bos.toByteArray();
//                    String a = new String(data);
                    Jedis jedis = new Jedis("localhost");
                    byte[] data = jedis.get("image01".getBytes());
                    ByteArrayInputStream bis = new ByteArrayInputStream(data);
                    BufferedImage img = ImageIO.read(bis);
                    ImageIcon icon = new ImageIcon(img);
                    label1.setIcon(icon);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                    JOptionPane.showMessageDialog(null,"请检查是否没有开启localhost：6739的redis服务\n以及是否点击过存入");

                }

            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("RedisSavingPhoto");
        frame.setContentPane(new RedisSavingPhoto().mainpanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(740,500);
        frame.setVisible(true);
    }

    public byte[] ImageToByteArray() throws IOException {
        BufferedImage bImage = ImageIO.read(new File(IMG_PATH));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(bImage, "jpg", bos);
        byte[] data = bos.toByteArray();
        return data;
    }

}
